
package com.xg.controller;

import com.xg.model.entity.OneData;
import com.xg.model.entity.TwoData;
import com.xg.model.enums.DefaultEnums;
import com.xg.util.excel.ExcelTemplateExporter;
import com.xg.util.export.SheetData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/excel")
public class ExcelExportController {

    @GetMapping("/export")
    public String exportExcel() {

        String templateName = "easy_excel_complex_export.xlsx";
        String exportFileName = "测试模板导出.xlsx";

        List<SheetData<?>> sheetDataList = new ArrayList<>();

        // 第一个 Sheet 数据
        List<OneData> oneDataList = generateFirstSheetData();
        Map<String, Object> headerInfo1 = createHeaderInfo("这是第一页的标题", "这里是备注信息", "这里是查询条件");
        sheetDataList.add(new SheetData<>("第一页", oneDataList, OneData.class, headerInfo1, "查询条件"));

        // 第二个 Sheet 数据
        List<TwoData> twoDataList = generateSecondSheetData();
        Map<String, Object> summaryInfo = createSummaryInfo("2020-03-14", "100");
        sheetDataList.add(new SheetData<>("第二页", twoDataList, TwoData.class, summaryInfo,"查询条件"));

        ExcelTemplateExporter.exportWithTemplate(templateName, exportFileName, sheetDataList);

        return "导出成功";
    }

    private List<OneData> generateFirstSheetData() {
        List<OneData> data = new ArrayList<>();
        for (int i = 1; i <= 1000; i++) {
            OneData item = new OneData(i, "测试0" + i, String.valueOf(10 + i));
            if (i == 1) {
                item.setOperationType(DefaultEnums.UPDATE.getMsg());
                item.setModifiedField("name");
            } else if (i == 2) {
                item.setOperationType(DefaultEnums.DELETE.getMsg());
            } else if (i == 3) {
                item.setOperationType(DefaultEnums.INSERT.getMsg());
            }
            data.add(item);
        }
        return data;
    }

    private List<TwoData> generateSecondSheetData() {
        List<TwoData> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TwoData item = new TwoData(i, "测试0" + i, "2" + i);
            if (i == 3) {
                item.setOperationType(DefaultEnums.UPDATE.getMsg());
                item.setModifiedField("name");
            } else if (i == 4) {
                item.setOperationType(DefaultEnums.DELETE.getMsg());
            } else if (i == 6) {
                item.setOperationType(DefaultEnums.INSERT.getMsg());
            }
            data.add(item);
        }
        return data;
    }

    private Map<String, Object> createHeaderInfo(String title, String remark, String criteria) {
        Map<String, Object> info = new HashMap<>();
        info.put("title", title);
        info.put("remark", remark);
        info.put("queryCriteria", criteria);
        return info;
    }

    private Map<String, Object> createSummaryInfo(String date, String total) {
        Map<String, Object> info = new HashMap<>();
        info.put("date", date);
        info.put("total", total);
        return info;
    }
}